package cn.wolfcde.luowowo.website.web.controller;

import cn.wolfcde.luowowo.website.util.CookieUtil;
import cn.wolfcode.luowowo.common.domain.AjaxResult;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class UserInfoController {

    @Reference
    private IUserInfoService userInfoService;

    /**
     * 验证手机号码唯一性
     * @param phone 手机号
     * @return  true 唯一 false 不唯一
     */
    @RequestMapping("checkPhone")
    @ResponseBody
    public Object checkPhone(String phone){

        return userInfoService.checkPhone(phone);

    }

    /**
     * 发送验证码
     * @param phone 手机号
     * @return  操作结果
     */
    @RequestMapping("sendVerifyCode")
    @ResponseBody
    public AjaxResult sendVerifyCode(String phone){
        AjaxResult ajaxResult = new AjaxResult();

        userInfoService.sendVerifyCode(phone);

        return ajaxResult;

    }

    /**
     * 用户注册
     * @param phone 手机号 账号
     * @param nickname  昵称
     * @param password  密码
     * @param rpassword   二次密码
     * @param verifyCode    验证码
     */
    @RequestMapping("userRegist")
    @ResponseBody
    public AjaxResult userRegist(String phone,String nickname,String password,String rpassword,String verifyCode){
        AjaxResult ajaxResult = new AjaxResult();

        userInfoService.userRegist(phone,nickname,password,rpassword,verifyCode);



        return ajaxResult;

    }

    /**
     * 用户登录的接口
     * @param username  用户名
     * @param password  密码
     */
    @RequestMapping("userLogin")
    @ResponseBody
    public AjaxResult userLogin(String username, String password, HttpServletResponse response){
        AjaxResult ajaxResult = new AjaxResult();

        String token = userInfoService.userLogin(username, password);

        //将令牌使用token返回给前台
        CookieUtil.addCookie(token,response);


        return ajaxResult;

    }
}
